package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.RackCodeEntity;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.module.dam.entity.dev.RackInfo;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.suite.core.util.ContextUtils;

public class OpenMicroenvironmentPageHandle extends AbstractScreenCommandHandle{
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private final static short RACK_CODE_ID = 9;
	private final static short ASSET_COUNT_ID = 11;
	private final static short MICROENVIROMENT_SCREEN_ID = 1;
	@Override
	protected String getCommandCode() {
		return "EEB11100000005100000";
	}

	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		RackCodeEntity entity = (RackCodeEntity) msg.getAwsPostdata();
		RackInfo rackInfo = damService.qryRackInfoByDevNo(entity.getRackConverCode());
		if(rackInfo!=null) {
			try {
				sendRackInfoToScreen(rackInfo,entity.getRackConverCode());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void sendRackInfoToScreen(RackInfo rackInfo, String rackConverCode) throws Exception {
		Map<Short,String> map = new HashMap<>();
		map.put(RACK_CODE_ID, rackInfo.getRackName());
		map.put(ASSET_COUNT_ID, String.valueOf(rackInfo.getAssetCount()));
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(rackConverCode, MICROENVIROMENT_SCREEN_ID, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}

}
